package com.example.love_margin.Service.Impl;

import com.example.love_margin.Entity.Pet;
import com.example.love_margin.Mapper.PetMapper;
import com.example.love_margin.Mapper.Until.ImgMapper;
import com.example.love_margin.Service.Impl.Until.delPetsUtil;
import com.example.love_margin.Service.PetService;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.forDelete;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PetServiceImpl implements PetService {

    @Autowired
    PetMapper petMapper;
    @Autowired
    ImgMapper imgMapper;

    //全局变量，方便更改图片绝对存储目录（包含图片位置文件夹）
    @Value("${web.imgURL}")
    private String imgURL;

    //全局变量，方便更改图片删除目录（不包含图片位置文件夹）
    @Value("${web.imgURL2}")
    private String imgURL2;

    //全局变量，方便更改图片相对存储目录
    @Value("${web.imgURL1}")
    private String imgURL1;

    //创建删除批量文件的实例化
    @Autowired
    forDelete delete = new forDelete();

    @Autowired
    delPetsUtil delP = new delPetsUtil();

    @Override
    public Object addPet(Pet pet) {

        if (petMapper.addPet(pet)==1){
            String Url = pet.getPet_picture();
            if(Url!=null){
                // 利用split方法指定按照逗号切割字符串
                String[] UrlArray = Url.split(",");
                List<String> MK = new ArrayList<>();
                MK = Arrays.asList(UrlArray);
                for (int i = 0; i < MK.size(); i++) {
                    if (MK.get(i) != null) {
                        imgMapper.upPetOldUrl(MK.get(i));
                    } else {
                        System.out.println("失败");
                    }
                }
            }
            return ResultCode.SUCCESS.result("增加成功");
        }else {
            return ResultCode.SUCCESS.result(false);
        }

    }

    @Override
    public Object deletePet(int pet_id) {
        String pet_url = petMapper.selectPetUrl(pet_id);
        if(pet_url.length() > 0){
            // 利用split方法指定按照逗号切割字符串
            String[] UrlArray = pet_url.split(",");
            List<String> MK = new ArrayList<>();
            MK = Arrays.asList(UrlArray);
            if(delete.DeleteList(MK)){
                if(petMapper.deletePet(pet_id)==1){
                    return ResultCode.SUCCESS.result("删除成功");
                }else {
                    return ResultCode.SUCCESS.result("删除失败");
                }
            }
            return ResultCode.SUCCESS.result("删除失败");
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object delPets(Integer[] pet_ids) {
//        List<Pet> pet_id = (List<Pet>) map.get("pet_id");
        String URLs = delP.admin("pet_picture", "pet", "pet_id", pet_ids);
        String sql = delP.deleSQL("pet", "pet_id", pet_ids);
        List<String> aa = petMapper.selectPetAll(URLs);
        String ms = "";
        List<String> url = delete.DeleteList2(aa);
        if (delete.DeleteList(url)) {
            if (petMapper.deletePets(sql) >= 1) {
                try {
                    ms = "删除成功";
                } catch (Exception e) {
                    System.out.println(e);
                }
                return ResultCode.SUCCESS.result(ms);
            } else {
                return ResultCode.SUCCESS.result("删除失败");
            }
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object updateOnePet(Pet pet) {
        if (petMapper.upOnePet(pet)==1){
            return ResultCode.SUCCESS.result("修改成功");
        }else {
            return ResultCode.SUCCESS.result("修改失败");
        }
    }


    @Override
    public Object selectOnePet(int pet_id) {

            return ResultCode.SUCCESS.result(petMapper.selectOnePetAll(pet_id));

    }

    @Override
    public Object selectAll(int pet_id) {

        String test = pet_id + "1";
        if(!test.equals("1")){
            pet_id = pet_id+1;
                return ResultCode.SUCCESS.result(petMapper.selectLast(pet_id));

        }else {
            return ResultCode.SUCCESS.result(petMapper.selectTen());
        }
    }

    @Override
    public Object selectAllTen() {
        return ResultCode.SUCCESS.result(petMapper.selectTen());
    }

    @Override
    public Object selectXAll(int pet_id, int num) {
        String test = pet_id + "1";
        String test2 = num + "1";
        if(!test.equals("1")&!test2.equals("1")){
            pet_id = pet_id+1;
            return ResultCode.SUCCESS.result(petMapper.selectLast2(pet_id,num));

        }else {
            return ResultCode.SUCCESS.result(petMapper.selectTen());
        }
    }

    @Override
    public Object selectManyKey(String pet_category, String pet_varieties, String pet_sex) {
        return ResultCode.SUCCESS.result(petMapper.selectManyKey(pet_category,pet_varieties,pet_sex));
    }

    @Override
    public Object selectManyKeyAdmin(String pet_category, String pet_varieties, String pet_sex, String key) {
        return ResultCode.SUCCESS.result(petMapper.selectManyKeyAd(pet_category,pet_varieties,pet_sex,key));
    }

    @Override
    public List<Pet> selectManyPage(String pet_category, String pet_varieties, String pet_sex, String key, Integer pageNum, Integer pageSize) {
        //用插件进行分页
        PageHelper.startPage(pageNum, pageSize);
        return petMapper.selectManyPage(pet_category,pet_varieties,pet_sex,key);
    }


}
